﻿using AkazoNobel.Scheduler.StoreMasterData.Common;
using AkazoNobel.Scheduler.StoreMasterData.DataAccess.Model;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Dapper;

namespace AkazoNobel.Scheduler.StoreMasterData.DataAccess.Service
{
    public class SyncPoolService
    {
        /// <summary>
        /// 日志记录
        /// </summary>
        private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public static List<SyncPoolModel> GetList()
        {
            List<SyncPoolModel> syncList;

            //1.先从EApprove数据库中抓取最近一周的审核数据 (tinting数据库没有权限建视图，建在mdc库中)
            using (var connSMD = SqlConnectionHelper.GetConnection(Constant.SMD_DB_NAME))
            {
                syncList = connSMD.Query<SyncPoolModel>("SELECT * FROM t_smd_SmdSyncData where SyncStatus <> @SyncStatus ",
                        new { SyncStatus = SyncStatus.Success }).ToList();
            }

            //2.与MDC数据库中比对，这条审核数据有没有抓取过
            var results = new List<SyncPoolModel>();
            using (var connMdc = SqlConnectionHelper.GetConnection(Constant.MDC_DB_NAME))
            {
                foreach (var sync in syncList)
                {
                    var count = connMdc.Query<int>(
                        "SELECT COUNT(ID) FROM t_StoreMasterData WHERE SerialNumber=@SerialNumber",
                        new { SerialNumber = sync.SerialNumber }).Single();

                    if (count == 0)
                    {
                        if (!string.IsNullOrEmpty(sync.CertiCode))
                        {
                            sync.CertiCode = sync.CertiCode.PadLeft(8, '0');
                        }
                        results.Add(sync);
                    }
                }
            }

            return results;
        }

        internal static string GetSMDInsertSql(Store store)
        {
            StringBuilder filedSB = new StringBuilder();
            StringBuilder valueSB = new StringBuilder();
            Type type = store.GetType();
            //通过GetProperties方法可以获取该类下的所有成员
            foreach (PropertyInfo pi in type.GetProperties())
            {
                string nameStr = pi.Name;
                var value = pi.GetValue(store, null);

                if (value != null && SyncValidate(value))
                {
                    filedSB.Append(nameStr + ", ");
                    valueSB.Append("@" + nameStr + ", ");
                }
            }


            string fieldStr = filedSB.ToString().Substring(0, filedSB.ToString().LastIndexOf(','));

            string valueStr = valueSB.ToString().Substring(0, valueSB.ToString().LastIndexOf(','));

            StringBuilder sqlSB = new StringBuilder();
            sqlSB.Append("insert into t_StoreMasterData(");
            sqlSB.Append(fieldStr + ") values(");
            sqlSB.Append(valueStr + "); ");

            return sqlSB.ToString();

        }

        private static bool SyncValidate(object syncData)
        {
            bool result = true;
            Type type = syncData.GetType();
            DateTime dateTime = new DateTime();
            Decimal dec = new decimal();

            if (type == typeof(string))
            {
                if ((string)syncData == string.Empty)
                {
                    result = false;
                }
            }
            else if (type == typeof(int))
            {
                if ((int)syncData == 0)
                {
                    result = false;
                }
            }
            else if (type == typeof(double))
            {
                if ((double)syncData == 0.00)
                {
                    result = false;
                }
            }
            else if (type == typeof(decimal))
            {
                if ((decimal)syncData == dec)
                {
                    result = false;
                }
            }
            else if (type == typeof(DateTime))
            {
                if ((DateTime)syncData == dateTime)
                {
                    result = false;
                }
            }

            return result;
        }
        }
    
}
